package com.fast.manager.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fast.commons.spring.SpringContextHolder;
import com.fast.commons.web.model.AjaxResponse;
import com.fast.commons.web.model.DataGrid;
import com.fast.manager.system.domain.MenuPermit;
import com.fast.manager.system.repository.MenuPermitMapper;
import com.fast.manager.system.service.MenuPermitService;

@Service
public class MenuPermitServiceImpl implements MenuPermitService {
	
	@Autowired
	private MenuPermitMapper mapper ;
	
	@Override
	public AjaxResponse add(MenuPermit record) {
		this.mapper.insertSelective(record);
		
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse delete(long id) {
		this.mapper.deleteByPrimaryKey(id);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse deleteBatch(Long[] ids) {
		this.mapper.deleteByPrimaryKeys(ids);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse update(MenuPermit record) {
		this.mapper.updateByPrimaryKeySelective(record);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public MenuPermit get(long id) {
		return this.mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<MenuPermit> getAllList(MenuPermit record) {
		return this.mapper.findAllList(record);
	}
	
	@Override
	public DataGrid<MenuPermit> datagrid(MenuPermit conditions) {
		DataGrid<MenuPermit> dg = new DataGrid<MenuPermit>();
		
		Map<String, Object> conditionsMap = new HashMap<String, Object>();
		conditionsMap.put("record", conditions);
		conditionsMap.put("offset", dg.getOffset());
		conditionsMap.put("rows", dg.getPageSize());

		List<MenuPermit> list = this.mapper.findAllListPage(conditionsMap);
		int count = this.mapper.getCount(conditionsMap);
		dg.setRows(list);
		dg.setTotal(count);

		return dg;
	}

	@Override
	public void insertBatch(Long menu_id, List<Long> permitIds) {
		List<MenuPermit> recordBatch = new ArrayList<MenuPermit>();
		for (Long permitId : permitIds) {
			MenuPermit r = new MenuPermit() ;
			r.setMenu_id(menu_id);
			r.setPermit_id(permitId);
			recordBatch.add(r) ;
		}
		this.mapper.insertBatch(recordBatch);
	}

	@Override
	public void deleteByMenuId(Long menu_id) {
		this.mapper.deleteByMenuId(menu_id);
	}

}
